﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AntdUI;

namespace TPressure
{
    public partial class ManageProjectForm : BaseForm
    {
        #region 属性
        /// <summary>设备
        /// 
        /// </summary>
        private Project obj;

        #endregion

        #region 自定义方法

        /// <summary>设备列表
        /// 
        /// </summary>
        public void LoadProjectList()
        {
            List<Project> list = SqlSugarHelper.Inquireable<Project>(null);
            UpdateTable(list);
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateTable(List<Project> list)
        {
            var antItemList = new List<ProjectItem>();
            foreach (Project item in list)
            {

                antItemList.Add(new ProjectItem(item.project_number, item.project_name, item.stake_mark, item.pipe_specification,
                                                 item.pipe_length, item.medium, item.design_pressure, item.pipe_nature, item.test_type,
                                                 item.company, item.manometer_precision, item.manometer_range, item.template_id));
            }
            Table1.DataSource = antItemList;
        }

        /// <summary>根据编号查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Project ProjectWithNumber(string number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["project_number"] = number;
            List<Project> list = SqlSugarHelper.Inquireable<Project>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckSelectedRowExist(string number)
        {
            obj = ProjectWithNumber(number);
            if (obj == null)
            {
                Alert.Error("模板记录不存在", this);
                return false;
            }
            else
            {
                return true;
            }
        }

        #endregion
        public ManageProjectForm()
        {
            InitializeComponent();
            this.FullScreen = true;
        }

        private void ManageProjectForm_Load(object sender, EventArgs e)
        {
            Table1.Height = this.Height - 40;
            Table1.Columns = new AntdUI.ColumnCollection {
                new AntdUI.Column("project_number","工程编号"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("project_name","工程名称"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("stake_mark","桩号"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("pipe_specification","管道规格"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("pipe_length","管道长度"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("medium","试压介质"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("design_pressure","设计压力"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("pipe_nature","试压性质"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("test_type","试压类型"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("company","施工单位"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("manometer_precision","压力表精度"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("manometer_range","压力表量程"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("template_id","模板ID"){ColAlign=AntdUI.ColumnAlign.Center},
                new AntdUI.Column("operation","操作"){ Fixed=true,Width="auto",ColAlign=AntdUI.ColumnAlign.Center}
            };
            Spin.Config config = new Spin.Config
            {
                Text = "正在加载中...",
            };
            Table1.Spin(config, () =>
            {
                LoadProjectList();
            }, () =>
            {

            });
        }

        private void Table1_CellButtonClick(object sender, AntdUI.TableButtonEventArgs e)
        {
            if (e.Record is ProjectItem data)
            {

                AntdUI.CellLink link = e.Btn;
                if (link.Id == "edit")
                {
                    EditProjectForm frm = new EditProjectForm();
                    frm.project_number = data.project_number;
                    frm.Owner = this;
                    frm.ShowDialog();
                }
                else if (link.Id == "delete")
                {
                    AntdUI.Modal.open(new AntdUI.Modal.Config(this, "友情提示", "您确定要删除此工程吗？")
                    {
                        OnOk = config =>
                        {
                            bool res = CheckSelectedRowExist(data.project_number);
                            if (!res)
                            {
                                return true;
                            }
                            List<Project> list = new List<Project>();
                            list.Add(obj);
                            int deleteRes = SqlSugarHelper.Deleteable(list);
                            if (deleteRes > 0)
                            {
                                LoadProjectList();
                            }
                            else
                            {
                                Alert.Error("删除工程失败", this);
                            }
                            return true;
                        },
                    });
                }
            }
        }
    }
}
